﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
using WCFMINERP.Contracts.DataContracts;


namespace WCFMINERP.ContextPropagation
{

    [Serializable]
    public class ApplicationContext : Dictionary<string, object>
    {
        private const string CallContextKey = "__ApplicationContext";
        internal const string ContextHeaderLocalName = "__ApplicationContext";
        internal const string ContextHeaderNamespace = "WCFMINERP";

        private void EnsureSerializable(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!value.GetType().IsSerializable)
            {
                throw new ArgumentException(string.Format("The argument of the type \"{0}\" is not serializable!", value.GetType().FullName));
            }
        }

        public new object this[string key]
        {
            get
            {
                return base[key];
            }
            set
            {
                this.EnsureSerializable(value);
                base[key] = value;
            }
        }
        #region //属性
        //public ApplationParas AlphaPara
        //{
        //    get
        //    {
        //        return (ApplationParas)this["__AlphaPara"];
        //    }
        //    set
        //    {
        //        this["__AlphaPara"] = value;
        //    }
        //}
        /// <summary>
        /// 商户ID
        /// </summary>
        public int Shopid
        {
            get
            {
                return (int)this["__Shopid"];
            }
            set
            {
                this["__Shopid"] = value;
            }
        }
        /// <summary>
        /// 商户名称
        /// </summary>
        public string ShopName
        {
            get
            {
                return (string)this["__ShopName"];
            }
            set
            {
                this["__ShopName"] = value;
            }
        }
        /// <summary>
        /// 登录名
        /// </summary>
        public string UName
        {
            get
            {
                return (string)this["__UName"];
            }
            set
            {
                this["__UName"] = value;
            }
        }
        /// <summary>
        /// 登录密码
        /// </summary>
        public string Upwd
        {
            get
            {
                return (string)this["__Upwd"];
            }
            set
            {
                this["__Upwd"] = value;
            }
        }
        /// <summary>
        /// 用户授权码
        /// </summary>
        public string SHACODE
        {
            get
            {
                return (string)this["__SHACODE"];
            }
            set
            {
                this["__SHACODE"] = value;
            }
        }
        /// <summary>
        /// 客户端版本号
        /// </summary>
        public string ClientVersion
        {
            get
            {
                return (string)this["__ClientVersion"];
            }
            set
            {
                this["__ClientVersion"] = value;
            }
        }
        /// <summary>
        /// 产品列表版本号
        /// </summary>
        public string ProductVersion
        {
            get { return (string)this["__ProductVersion"]; }
            set { this["__ProductVersion"] = value; }
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline
        {
            get
            {
                return (bool)this["__CIsOnline"];
            }
            set
            {
                this["__IsOnline"] = value;
            }
        }
        /// <summary>
        /// 当前电脑名称
        /// </summary>
        public string MachineName
        {
            get
            {
                return (string)this["__MachineName"];
            }
            set
            {
                this["__MachineName"] = value;
            }
        }
        #endregion

        public static ApplicationContext Current
        {
            get
            {
                if (CallContext.GetData(CallContextKey) == null)
                {
                    CallContext.SetData(CallContextKey, new ApplicationContext());
                }

                return CallContext.GetData(CallContextKey) as ApplicationContext;
            }
            set
            {
                CallContext.SetData(CallContextKey, value);
            }
        }
    }
}
